import commonConstants from "@/common/commonConstants";

export default class Marker{
    markerId;
    markerCode;
    markerName;
    markerType;
    markerEntity;
    markerPositions;
    markerControlPoints;
    isCompositeEntity;
    isShow;
    layerId;

    constructor(options){
        const _this = this;
        _this.isCompositeEntity = false;
        _this.isShow = true;
        _this.markerControlPoints = [];
        _this.layerId = "-1";
        if (options){
            if (options["markerId"]){
                _this.markerId = options["markerId"];
            }
            if (options["isCompositeEntity"]){
                _this.isCompositeEntity = options["isCompositeEntity"];
            }
            if (options["markerName"]){
                _this.markerName = options["markerName"];
            }
            if (options["entity"]){
                _this.markerEntity = options["entity"];
            }
            if (options["markerCode"]){
                _this.markerCode = options["markerCode"];
                _this.markerType = _this.markerCode.getMarkerType()
            }
            if (options["position"]){
                _this.markerPositions = options["position"];
            }
        }
    }

    /**
     * 返回标绘的ID
     * @returns 
     */
    getMarkerId(){
        const _this = this;
        return _this.markerId;
    }
    getID(){
        let _this = this;
        return _this.markerId;
    }

    setLayerId(lid){
        this.layerId = lid;
    }

    setMarkerName(markerName){
        let _this = this;
        //地球上的文字改变
        if (_this.isCompositeEntity){
            _this.markerEntity.setAnnotationText(markerName);
        }else{
            if (_this.markerEntity.label){
                _this.markerEntity.label.text = markerName;
            }
        }
        _this.markerName = markerName;
    }

    getMarkerName(){
        return this.markerName;
    }


    getEntity() {
        let _this = this;
        return _this.markerEntity;
    }

     /**
     * 设置控制点
     * @param cps {*[]}
     */
     setControlPoints(cps){
        let _this = this;
        for (let i = 0; i < cps.length; i++) {
            _this.markerControlPoints.push(cps[i]);
        }
    }

    getControlPoints(){
        return this.markerControlPoints;
    }

    setControlPointShow(bool){
        let _this = this;
        for (let i = 0; i < _this.markerControlPoints.length; i++) {
            _this.markerControlPoints[i].show = bool;
        }
    }

    setPositions(pos){
        let _this = this;
        _this.markerPositions = pos;
    }

    getPositions(){
        return this.markerPositions;
    }

    getMerkerType(){
        return this.markerType;
    }

    /**
     * @param {*} positions 需要改变的点集合
     */
    updatePositions(positions){
        let _this = this;
        if (_this.isCompositeEntity){
            _this.markerEntity.movePositon(positions);
        }else{
            _this.markerEntity.position = positions;
        }
        _this.markerPositions = positions;
    }

    setShow(bool){
        let _this = this;
        if (_this.isCompositeEntity){
            _this.markerEntity.setShow(bool);
        }else{
            _this.markerEntity.show = bool;
        }
        _this.isShow = bool;
    }
    
    destroy(curViewer){
        let _this = this;
        if (_this.isCompositeEntity){
            _this.markerEntity.destroy();
        }else{
            curViewer.entities.remove(_this.markerEntity);
        }
    }
    
    toJson() {
        let _this = this;
        let markerPossJson = [];
        if ($.type(_this.markerPositions).toLowerCase() === "array"){
            markerPossJson = CommonMethod.toJsonForPositions(_this.markerPositions);
        }else{
            markerPossJson = CommonMethod.toJsonForPosition(_this.markerPositions);
        }
        return {
            markerId:_this.markerId,
            markerName:_this.markerName,
            markerType:_this.markerType,
            markerCode:_this.markerCode.getCode(),
            markerPositions:markerPossJson,
            customParameters:_this.customParameters.toJson(),
            isCompositeEntity:_this.isCompositeEntity?"true":"false",
            layerId:_this.layerId,
            scenarioId:scenario_deduce.getCurScenario().getScenarioId()
        }
    }
};